package com.lbd.gulimall.search.controller;

import com.lbd.common.exception.BizCodeEnum;
import com.lbd.common.to.SkuEsModel;
import com.lbd.common.utils.R;
import com.lbd.gulimall.search.service.ElasticSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @Author: linbd
 * @Description:
 * @Date: 2023/2/11 10:48
 */
@Slf4j
@RequestMapping(("search/save"))
@RestController
public class ElasticSearchController {
    @Autowired
    private ElasticSearchService elasticSearchService;

    @PostMapping("/product")
    public R bulkIndexSkuModel(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean isComplete;
        try {
            isComplete = elasticSearchService.bulkIndexSkuModel(skuEsModels);
        } catch (IOException e) {
            log.error("商品上架发生错误，{}", e);
            return R.error(BizCodeEnum.PRODUCT_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_EXCEPTION.getMsg());
        }
        if (isComplete) {
            return R.ok();
        } else {
            return R.error(BizCodeEnum.PRODUCT_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_EXCEPTION.getMsg());
        }
    }
}
